open Functions

type json = 
  | JsonObject of (string option) * (string * json) list
  | JsonArray of json list
  | JsonNumber of string
  | JsonString of string
  | JsonFunction of string
  | JsonTrue
  | JsonFalse
  | JsonNull

let rec json_to_string indent = function
  | JsonTrue -> "true"
  | JsonFalse -> "false"
  | JsonNull -> "null"
  | JsonNumber x -> x
  | JsonString x -> "'" ^ x ^ "'"
  | JsonFunction x -> "function(){ " ^ x ^ "}"
  | JsonArray xx -> "[\n" ^ (indent ^ "  ") ^ (join ", " (map (json_to_string (indent ^ "  ")) xx)) ^ "\n" ^ indent ^ "]" 
(*
  | JsonArray xx -> "[" ^ (json_list_to_string (indent ^ "  ") xx) ^ "]"
*)

  | JsonObject (None, xx) ->
  		"{\n" 
  		^ join ",\n" (map (function (k, v) -> (indent ^ "  ") ^ k ^ ": " ^ (json_to_string (indent ^ "  ") v)) xx)
  		^ "\n" ^ indent ^ "}"

  | JsonObject (Some comment, xx) ->
  		"/*" ^ comment ^ "*/\n" ^ indent ^ "{\n" 
  		^ join ",\n" (map (function (k, v) -> (indent ^ "  ") ^ k ^ ": " ^ (json_to_string (indent ^ "  ") v)) xx)
  		^ "\n" ^ indent ^ "}"


(*
and json_list_to_string indent = function
  | [] -> ""
  | (JsonComment x) :: tl -> "\n" ^ indent ^ "/*" ^ x ^ "*/\n" ^ indent ^ (json_list_to_string indent tl)
  | x :: tl -> (json_to_string indent x) ^ ", " ^ (json_list_to_string indent tl)
*)
